package com.jian.supernote.module.setting.feedback;

import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;

import com.jian.supernote.bean.BaseResponse;
import com.jian.supernote.http.JsonCallback;
import com.jian.supernote.model.feedback.FeedbackModel;
import com.jian.supernote.model.feedback.IFeedback;
import com.jian.supernote.module.base.BasePresenter;
import com.lzy.okgo.model.Response;

/**
 * <pre>
 *     author : FaDai
 *     e-mail : i_fadai@163.com
 *     time   : 2017/06/29
 *     desc   : xxxx描述
 *     version: 1.0
 * </pre>
 */

public class FeedbackPresenter extends BasePresenter<IFeedbackView> implements IFeedbackPresenter{

    private String mLastContent="",mLastContact="";
    IFeedback feedbackModel;

    public FeedbackPresenter() {
        feedbackModel = new FeedbackModel();
    }

    @Override
    public void sendFeedback(final String content, final String contact) {

        if(TextUtils.isEmpty(content)){
            ToastUtils.showShort("请至少填写反馈内容");
            return;
        }
        if(content.equals(mLastContent) && contact.equals(mLastContact)){
            ToastUtils.showShort("请不要重复提交");
            return;
        }
        mView.showLoading();
        feedbackModel.commitFeedback(content, contact, new JsonCallback<BaseResponse>() {
            @Override
            public void onSuccess(Response<BaseResponse> response) {
                if(response.body().getStatus() == 1) {
                    mView.commitSucceed();
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();
                mView.cancleLoading();
            }
        });
//        Feedback feedback=getFeedback(content,contact);
//        mView.showLoading();
//        feedback.save(new SaveListener<String>() {
//            @Override
//            public void done(String s, BmobException e) {
//               sendBack(e,content,contact);
//            }
//        });
    }

//    private Feedback getFeedback(String content,String contact){
//        Feedback feedback=new Feedback();
//        feedback.setContent(content);
//        feedback.setContact(contact);
//        feedback.setSdk(DeviceUtils.getSDKVersion());
//        feedback.setVersion(AppUtils.getAppVersionName());
//        return feedback;
//    }
//
//    private void sendBack(BmobException e,String content,String contact){
//        mView.cancleLoading();
//        if(e==null){
//            mLastContent=content;
//            mLastContact=contact;
//            ToastUtils.showShort("提交成功");
//        } else{
//            ToastUtils.showShort("提交失败");
//        }
//    }

    @Override
    public void attch(IFeedbackView mView) {
        super.attch(mView);
    }

    @Override
    public void detach() {
        super.detach();
    }
}
